package com.tongfusoft.document.manager.user.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import com.tongfuosft.document.system.service.IDocSystempParamService;
import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.entity.AccountHasUsers;
import com.tongfusoft.document.entity.RoleHasUsers;
import com.tongfusoft.document.entity.SystemParam;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.user.service.IDocumentUserService;
import com.tongfusoft.document.system.service.impl.DocSystemParamServiceImpl;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.data.User;
import com.tongfusoft.utils.CryptoUtils;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("DocumentUserService")
public class DocumentUserServiceImpl extends DataTemplateServiceImpl implements
		IDocumentUserService {

	@Autowired
	@Qualifier(value = "DocumentUserService")
	public IDocumentUserService userService;
	@Autowired
	@Qualifier(value = "docSystemParamService")
	public IDocSystempParamService systemParamService;
	/**
	 * 添加用户
	 */
	@Override
	public Users addUser(Users user, Users u) {
		if (super.selectList("userList", user).size() > 0) {
			throw new ServiceException("repeat_error");
		}
		
		SystemParam systemparam = this.systemParamService.getParamByCode("defaultPassword");
		String userPassword = systemparam.getSystemValue();
		
		user.setUserPassword(CryptoUtils.bcryptEncoder(userPassword));
		user.setStatus("1");
		user.setUserId(UIDGeneratorService.getUUID());
		user.setCreatedBy(u.getUserId());
		user.setCreatedDate(new java.sql.Timestamp(new java.util.Date()
				.getTime()));
		user.setLastUpdBy(user.getUserId());
		user.setLastUpdDate(new java.sql.Timestamp(new java.util.Date()
				.getTime()));
		this.insert("addUser", user);
		return user;
	}

	/**
	 * 删除用户
	 */
	@Override
	public void delUser(Users user, Users u) {
		user.setLastUpdBy(u.getUserId());
		user.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.delete("delUser", user);
		this.userService.delRoleHasUserByUserId(user);
		this.userService.delAccountHasUserByUserId(user);
	}

	/**
	 * 编辑用户
	 */
	@Override
	public void updateUser(Users user, Users modiUser) {
		
		user.setUserPassword(CryptoUtils.bcryptEncoder(user.getUserPassword()));
		user.setLastUpdBy(modiUser.getUserId());
		user.setLastUpdDate(new java.sql.Timestamp(new java.util.Date()
				.getTime()));
		this.update("updateUser", user);
	}

	/**
	 * 重置密码
	 */
	@Override
	public void rePassword(Users user, Users modiUser) {
		SystemParam systemparam = this.systemParamService.getParamByCode("defaultPassword");
		String userPassword = systemparam.getSystemValue();
		user.setUserPassword(CryptoUtils.bcryptEncoder(userPassword));
		user.setLastUpdBy(modiUser.getUserId());
		user.setLastUpdDate(new java.sql.Timestamp(new java.util.Date()
				.getTime()));
		this.update("updateUser", user);
	}
	/**
	 * 编辑用户(不修改密码)
	 */
	@Override
	public void updateUserWithoutPwd(Users user, Users u) {
		user.setLastUpdBy(u.getUserId());
		user.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.update("updateUserWithoutPwd", user);
	}

	/**
	 * 获取所有用户
	 */
	@Override
	public List<Users> getUserList() {
		return this.selectList("allUsers");
	}

	/**
	 * 根据用户ID获取用户信息
	 */
	@Override
	public Users getUserById(String userId) {
		Users user = new Users();
		user.setUserId(userId);
		return (Users) this.selectOne("getUser", user);
	}
	
	/**
	 * 根据用户名获取用户信息
	 */
	@Override
	public Users getUserByLoginName(String loginName) {
		Users user = new Users();
		user.setLoginName(loginName);
		return (Users) this.selectOne("getUser", user);
	}
	/**
	 * 用户列表(分页)
	 */
	@Override
	public PageInfo<Users> usersPageTable(int pageIndex, int pageSize,
			Users users) {
		return super.findPage("userList", users, pageSize, pageIndex);
	}
	
	/**
	 * 获取用户对应的菜单
	 */
	@Override
	public Collection<GrantedAuthority> loadUserAuthoritiesByName(
			String loginName) {
		Collection<GrantedAuthority> result = new ArrayList<GrantedAuthority>();
		result.add(new SimpleGrantedAuthority("ROLE_user"));
		return result;
	}

	
	/**
	 * login
	 */
	@Override
	public String getPwdByName(String loginName) {
		String pwd = (String) this.selectOne("userPwd", loginName);
		if (StringUtils.isEmpty(pwd)) {

			throw new ServiceException("loginName_error");
		}
		return pwd;
	}

	
	/**
	 * 添加用户与角色关联
	 */
	public void saveRoleHasUser(String[] roleList,String userId,Users user){
		RoleHasUsers ru = new RoleHasUsers();
		for (String roleId : roleList) {
			if(roleId.equals("")){	
			}else{
				ru.setUserId(userId);
				ru.setRoleId(roleId);
				this.userService.createRoleHasUser(ru,user);
			}
		}
	}
	/**
	 * 添加用户与角色关联
	 */
	@Override
	public RoleHasUsers createRoleHasUser(RoleHasUsers roleHasUsers, Users user) {
		roleHasUsers.setRuId(UIDGeneratorService.getUUID());
		roleHasUsers.setStatus("1");
		roleHasUsers.setCreatedBy(user.getUserId());
		roleHasUsers.setCreatedDate(new java.sql.Timestamp(
				new java.util.Date().getTime()));
		roleHasUsers.setLastUpdBy(user.getUserId());
		roleHasUsers.setLastUpdDate(new java.sql.Timestamp(
				new java.util.Date().getTime()));
		this.insert("addRoleHasUsers", roleHasUsers);
		return roleHasUsers;
	}

	/**
	 * 根据角色ID删除用户与角色关联
	 */
	@Override
	public void delRoleHasUser(RoleHasUsers roleHasUsers, Users user) {
		roleHasUsers.setLastUpdBy(user.getUserId());
		roleHasUsers.setLastUpdDate(new java.sql.Timestamp(new java.util.Date()
				.getTime()));
		this.delete("delRoleHasUsers", roleHasUsers);
	}

	/**
	 * 根据用户ID删除用户与角色关联
	 */
	@Override
	public void delRoleHasUserByUserId(Users user) {
		user.setLastUpdBy(user.getUserId());
		user.setLastUpdDate(new java.sql.Timestamp(new java.util.Date()
				.getTime()));
		this.delete("delRoleHasUsersByUserId", user);
	}

	/**
	 * 对应用户与角色权限
	 */
	@Override
	public List<RoleHasUsers> getRoleHasUser(RoleHasUsers roleHasUsers) {
		return this.selectList("getRoleHasUsers", roleHasUsers);
	}

	
	
	/**
	 * 添加用户与账户权限
	 */
	@Override
	public AccountHasUsers createAccountHasUser(AccountHasUsers accountHasUsers, Users user) {
		accountHasUsers.setAuId(UIDGeneratorService.getUUID());
		
		accountHasUsers.setStatus("1");
		accountHasUsers.setCreatedBy(user.getUserId());
		accountHasUsers.setCreatedDate(new java.sql.Timestamp(
				new java.util.Date().getTime()));
		accountHasUsers.setLastUpdBy(user.getUserId());
		accountHasUsers.setLastUpdDate(new java.sql.Timestamp(
				new java.util.Date().getTime()));
		this.insert("addAccountHasUsers", accountHasUsers);
		return accountHasUsers;
	}

	/**
	 * 根据账户ID删除用户与账户权限
	 */
	@Override
	public void delAccountHasUser(AccountHasUsers accountHasUsers, Users user) {
		accountHasUsers.setLastUpdBy(user.getUserId());
		accountHasUsers.setLastUpdDate(new java.sql.Timestamp(
				new java.util.Date().getTime()));
		this.delete("delAccountHasUsers", accountHasUsers);
	}

	/**
	 * 根据用户ID删除用户与账户权限
	 */
	@Override
	public void delAccountHasUserByUserId(Users user) {
		user.setLastUpdBy(user.getUserId());
		user.setLastUpdDate(new java.sql.Timestamp(new java.util.Date()
				.getTime()));
		this.delete("delAccountHasUsersByUserId", user);
	}

	/**
	 * 对应用户与账户权限列表
	 */
	@Override
	public List<AccountHasUsers> getAccountHasUser(
			AccountHasUsers accountHasUsers) {
		return this.selectList("getAccountHasUsers", accountHasUsers);
	}
}
